package com.mdz.sbgl.Controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdz.sbgl.Entity.Gsxxb;
import com.mdz.sbgl.Entity.Sbflb;
import com.mdz.sbgl.Entity.Sbxxb;
import com.mdz.sbgl.Entity.Ygxxb;
import com.mdz.sbgl.Mapper.SbflbMapper;
import net.sf.ehcache.search.expression.Or;
import org.apache.commons.collections.SortedBidiMap;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/sbgl/sbflb")
public class SbflbController {

    @Autowired
    private SbflbMapper sbflbMapper;
    @Autowired
    private HttpSession session;

    @ResponseBody
    @RequestMapping(value = "/getsbflb/{gsid}",method = RequestMethod.GET)
    public List<Sbflb> getsbflb(@PathVariable("gsid") String gsid){
        return sbflbMapper.selectList(new QueryWrapper<Sbflb>().eq("gsid",gsid));
    }

    @ResponseBody
    @RequestMapping(value = "/maxbh/{sbid}",method = RequestMethod.GET)
    public String maxbh(@PathVariable("sbid") String sbid){
        List<Sbflb> sbflbs = sbflbMapper.selectList(new QueryWrapper<Sbflb>().eq("sbid",sbid));
        String i = Integer.toString(sbflbs.get(0).getMaxbh()+1);
        if (i.length()==2){
            return sbflbs.get(0).getSbid()+"0"+i;
        }else if (i.length()==1){
            return sbflbs.get(0).getSbid()+"00"+i;
        }else {
            return sbflbs.get(0).getSbid()+i;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updatesbflb/{sbid}", method = RequestMethod.PUT)
    public Integer updatesbflb(Sbflb sbflb, @PathVariable("sbid") String sbid) {
        System.out.println("这里sbid的值为："+sbid);
        System.out.println(sbflb);
        System.out.println("这里的最大编号的值为："+sbflb.getMaxbh());
        return sbflbMapper.update(sbflb, new UpdateWrapper<Sbflb>().eq("sbid", sbflb.getSbid()).eq("gsid",sbflb.getGsid()));
//        return sbflbMapper.updateById(sbflb);
    }

    @ResponseBody
    @RequestMapping(value = "/maxbhforint/{sbid}/{gsid}",method = RequestMethod.GET)
    public Integer maxbhforint(@PathVariable("sbid") String sbid,@PathVariable("gsid") String gsid){
        List<Sbflb> sbflbs = sbflbMapper.selectList(new QueryWrapper<Sbflb>().eq("sbid",sbid).eq("gsid",gsid));
        return sbflbs.get(0).getMaxbh()+1;
    }

    @ResponseBody
    @RequestMapping(value = "/getsbflblist",method = RequestMethod.GET)
    public String getsbflblist(HttpServletResponse response, @RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize, @RequestParam(value = "cxtj",required = false,defaultValue = "") String cxtj,@RequestParam(value = "gsid",required = false,defaultValue = "") String gsid){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
        String userid= session.getAttribute("userid").toString();
        List<Sbflb> ncList=new ArrayList<>();
        if (userid.equals("1")){
            ncList=sbflbMapper.selectList(new QueryWrapper<Sbflb>().like("gsid",cxtj).or().like("sbid",cxtj).or().like("sbmc",cxtj));
        }else {
            System.out.println(userid);
            ncList=sbflbMapper.selectList(new QueryWrapper<Sbflb>().eq("gsid",gsid));
        }
        PageInfo page=new PageInfo(ncList);
        List<Sbxxb> rows = page.getList();
        int total = (int)page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/insertsbflb",method = RequestMethod.POST)
    public int insertsbflb(Sbflb sbflb){
        return sbflbMapper.insert(sbflb);
    }
}

